package com.njtcedu.microblog.website.content.web;

import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.njtcedu.microblog.common.web.SessionConstants;
import com.njtcedu.microblog.website.content.service.ContentService;
import com.njtcedu.microblog.website.content.web.request.ContentSearchRequest;

@Controller
public class ContentController {

	@Inject
	private ContentService contentService;
	
	@RequestMapping(value = "/contentlist/{pageSize}", method = RequestMethod.POST)
	public String getContents(ContentSearchRequest request, HttpServletRequest httpRequest, @PathVariable("pageSize") Integer pageSize, Map<String, Object> map) {
		if (request != null) {
			httpRequest.getSession().setAttribute(SessionConstants.CONTENT_SEARCH_REQUEST, request);
		}
		
		map.put("pageModel", contentService.getAllContents(request, 1, pageSize));
		
		return "content_list";
	}
	
	@RequestMapping(value = "/contentlist/{pageNo}/{pageSize}", method = RequestMethod.GET)
	public String getContents(HttpServletRequest httpRequest, @PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize,  Map<String, Object> map) {
		ContentSearchRequest request = (ContentSearchRequest) httpRequest.getSession().getAttribute(SessionConstants.CONTENT_SEARCH_REQUEST);
		
		map.put("pageModel", contentService.getAllContents(request, pageNo, pageSize));
		
		return "content_list";
	}
	
	@RequestMapping(value = "/contents/list", method = RequestMethod.GET)
	public String getContents(Map<String, Object> map) {
		
		map.put("pageModel", contentService.getAllContents(null, 1, 15));
		
		return "content_list";
	}
}
